<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>使用Java生成二维码</title>
</head>
<body>
<h1>使用谷歌的ZXing开源库生成二维码</h1>
<hr>
<label for="url">请输入文本内容:</label>
<input type="text" id="url" placeholder="输入内容" />
<br>
<label for="type">选择二维码类型:</label>
<select id="type">
    <option value="black">黑白二维码</option>
    <option value="logo">带Logo的二维码</option>
    <option value="color">彩色二维码</option>
    <option value="background">背景图二维码</option>
    <option value="shape">特殊形状二维码</option>
    <option value="imageFill">图片填充二维码</option>
    <option value="gif">GIF动图二维码</option>
</select>
<br>
<label for="logo">上传Logo（可选）:</label>
<input type="file" id="logo" accept="image/*" />
<br>
<label for="bgImg">上传背景图（可选）:</label>
<input type="file" id="bgImg" accept="image/*" />
<br>
<button onclick="generateQRcode()">生成二维码</button>
<hr>
<img src="" id="qrcodeImg" alt="生成的二维码" />
<script>
    function generateQRcode() {
        const url = document.getElementById("url").value;
        const type = document.getElementById("type").value;
        const logoFile = document.getElementById("logo").files[0];
        const bgImgFile = document.getElementById("bgImg").files[0];

        const formData = new FormData();
        formData.append("url", url);
        formData.append("type", type);
        if (logoFile) {
            formData.append("logo", logoFile);
        }
        if (bgImgFile) {
            formData.append("bgImg", bgImgFile);
        }

        fetch("http://localhost:8080/qrcode/gen", {
            method: "POST",
            body: formData
        })
            .then(response => {
                if (response.ok) {
                    return response.blob();
                }
                throw new Error('Network response was not ok.');
            })
            .then(blob => {
                const imgUrl = URL.createObjectURL(blob);
                document.getElementById("qrcodeImg").src = imgUrl;
            })
            .catch(error => {
                console.error('There was a problem with the fetch operation:', error);
            });
    }
</script>
</body>
</html>
